﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 医院药库管理系统
{
    public partial class 药品出库单 : Form
    {
        private DataTable csTable1;
        public String name2;
        public 药品出库单()
        {
            InitializeComponent();
        }

        private void bt_ckfh_Click(object sender, EventArgs e)
        {
            首页 f3 = new 首页();
            f3.Show();
        }

        private void bt_cktj_Click(object sender, EventArgs e)
        {
            if (tb_ypbh.Text == "" || tb_ypmc.Text == ""||tb_szm.Text==""||tb_sccs.Text==""||tb_dj.Text==""||tb_sl.Text==""||cb_remark.Text==""||cb_ckzt.Text=="")
            {
                MessageBox.Show("信息不能为空！");
            }
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText =
                "INSERT tb_Out (No,MedicineName,Acronym,Made_Date,Valid_Date,OutOfDate,FirmName,Price,OutOfQuantity,DosageForm,Remark,Principal) VALUES(@No,@MedicineName,@Acronym,@Made_Date,@Valid_Date,@OutOfData,@FirmName,@Price,@OutOfQuantity,@DosageForm,@Remark,@Principal);";
            sqlCommand.Parameters.AddWithValue("@No", this.tb_ypbh.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@MedicineName", this.tb_ypmc.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Acronym", this.tb_szm.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Made_Date", this.dtp_scrq.Value);
            sqlCommand.Parameters.AddWithValue("@Valid_Date", this.dtp_yxq.Value);
            sqlCommand.Parameters.AddWithValue("@OutOfData", DateTime.Now);
            sqlCommand.Parameters.AddWithValue("@FirmName", this.tb_sccs.Text.Trim());
          
            sqlCommand.Parameters.AddWithValue("@Price", this.tb_dj.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@OutOfQuantity", this.tb_sl.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Remark", this.cb_remark.Text.Trim());
            
            sqlCommand.Parameters.AddWithValue("@DosageForm", this.cb_ckzt.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Principal",name2);
            SqlCommand sqlCommand1 = sqlConnection.CreateCommand();

             if (Convert.ToInt32(this.tb_sl.Text.ToString())<Convert.ToInt32(this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[9].Value.ToString()))
            {
                sqlCommand1.CommandText = "UPDATE tb_Medicine SET Quantity-=@Quantity WHERE Name=@Name1 And Valid_Date= @Valid_Date1;";
                sqlCommand1.Parameters.AddWithValue("@Name1", this.tb_ypmc.Text.Trim());
                sqlCommand1.Parameters.AddWithValue("@Valid_Date1", this.dtp_yxq.Value);
                sqlCommand1.Parameters.AddWithValue("@Quantity", Convert.ToInt32(this.tb_sl.Text.ToString()));
            }

             if (Convert.ToInt32(this.tb_sl.Text.ToString()) == Convert.ToInt32(this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[9].Value.ToString()))
             {

                 sqlCommand1.CommandText = "DELETE tb_Medicine WHERE Name=@Name1 And Valid_Date= @Valid_Date1;";
                 sqlCommand1.Parameters.AddWithValue("@Name1", this.tb_ypmc.Text.Trim());
                 sqlCommand1.Parameters.AddWithValue("@Valid_Date1", this.dtp_yxq.Value);
             }
            
            sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            int rowAffected1 = sqlCommand1.ExecuteNonQuery();
            sqlConnection.Close();
            if (rowAffected == 1 && rowAffected1==1)
            {
                MessageBox.Show("出库成功。");
            }
            else
            {
                MessageBox.Show("出库失败！");
            }
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void 药品出库单_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“yyaoDataSet8.tb_Physicalstate”中。您可以根据需要移动或移除它。
            this.tb_PhysicalstateTableAdapter.Fill(this.yyaoDataSet8.tb_Physicalstate);
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_Out;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_ck.DataSource = csTable;
            this.dgv_ck.Columns["No"].HeaderText = "编号";
            this.dgv_ck.Columns["MedicineName"].HeaderText = "名称";
            this.dgv_ck.Columns["Made_Date"].HeaderText = "生产日期";
            this.dgv_ck.Columns["Valid_Date"].HeaderText = "有效期至";
            this.dgv_ck.Columns["Acronym"].HeaderText = "首字母缩写";
            this.dgv_ck.Columns["FirmName"].HeaderText = "生产厂商";
            this.dgv_ck.Columns["OutOfDate"].HeaderText = "出库时间";
            this.dgv_ck.Columns["Price"].HeaderText = "价格";
            
            
            this.dgv_ck.Columns["DosageForm"].HeaderText = "物理状态";
            this.dgv_ck.Columns["Principal"].HeaderText = "负责人";
            this.dgv_ck.Columns["OutOfQuantity"].HeaderText = "数量";
            this.dgv_ck.Columns["Remark"].HeaderText = "出库去向";

            SqlCommand sqlCommand1 = new SqlCommand();
            sqlCommand1.Connection = sqlConnection;
            sqlCommand1.CommandText = "SELECT * FROM tb_Medicine";
            SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
            sqlDataAdapter1.SelectCommand = sqlCommand1;
            this.csTable1 = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter1.Fill(csTable1);
            sqlConnection.Close();
            this.dgv_kc.DataSource = csTable1;
            SqlCommand sqlph = new SqlCommand();
            sqlph.Connection = sqlConnection;
            sqlph.CommandText = "SELECT * FROM tb_Physicalstate;";
            SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();
            sqlDataAdapter2.SelectCommand = sqlph;
            DataTable Physicalstate = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter2.Fill(Physicalstate);
            sqlConnection.Close();


            DataGridViewComboBoxColumn PhysicalstateColumn = new DataGridViewComboBoxColumn();                  //声明并实例化数据网格视图下拉框列，用于设置先修课程；
            PhysicalstateColumn.Name = "PhysicalstateName";                                                             //设置下拉框列的名称；
            PhysicalstateColumn.HeaderText = "物理状态";                                                        //设置下拉框列的表头文本；
            PhysicalstateColumn.DataSource = Physicalstate;                                               //设置下拉框列的数据源为先修课程数据表；
            PhysicalstateColumn.DisplayMember = "PhysicalstateName";                                                         //设置下拉框列的显示成员为（先修课程数据表的）名称（列）；
            PhysicalstateColumn.ValueMember = "No";                                                             //设置下拉框列的值成员为（先修课程数据表的）编号（列）；
            PhysicalstateColumn.DataPropertyName = "Physicalstate";                                               //设置下拉框列的数据属性名称为（课程数据表的）先修课程编号（列）；
            PhysicalstateColumn.DisplayIndex = 13;                                                               //设置下拉框列的显示顺序；
            PhysicalstateColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;                             //设置下拉框列的自动调整列宽模式为填充；
            this.dgv_kc.Columns.Add(PhysicalstateColumn);
            this.dgv_kc.Columns["Acronym"].Visible = false;

            this.dgv_kc.Columns["Type"].Visible = false;
            //this.dgv_kc.Columns["PhysicalState"].Visible = false;
            this.dgv_kc.Columns["DosageForm"].Visible = false;
            this.dgv_kc.Columns["No"].HeaderText = "编号";
            this.dgv_kc.Columns["Name"].HeaderText = "名称";
            this.dgv_kc.Columns["Made_Date"].HeaderText = "生产日期";
            this.dgv_kc.Columns["Valid_Date"].HeaderText = "有效期至";
            this.dgv_kc.Columns["InDate"].HeaderText = "入库时间";
            this.dgv_kc.Columns["FirmName"].HeaderText = "生产厂商";
            this.dgv_kc.Columns["PurchasePrice"].HeaderText = "药品进价";
            this.dgv_kc.Columns["Price"].HeaderText = "零售价";
            this.dgv_kc.Columns["Type"].HeaderText = "药品类型";
            this.dgv_kc.Columns["PhysicalState"].HeaderText = "物理状态";
            this.dgv_kc.Columns["DosageForm"].HeaderText = "剂型";
            this.dgv_kc.Columns["in_principal"].HeaderText = "负责人";
            this.dgv_kc.Columns["Quantity"].HeaderText = "数量";
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void tb_ypmc_TextChanged(object sender, EventArgs e)
        {
            DataRow[] searchResultRows =
               this.csTable1.Select("Name LIKE '%" + this.tb_ypmc.Text.Trim() + "%'");             //借助本窗体的课程数据表的方法Select，并提供与SQL类似的谓词表达式作为查询条件，根据拼音缩写进行模糊查询（仅支持%通配符）；查询将返回数据行数组；
            DataTable searchResultTable = this.csTable1.Clone();                                     //借助本窗体的课程数据表的方法Clone，创建相同架构的空表，用于保存搜索结果所在数据行；
            foreach (DataRow row in searchResultRows)                                                       //遍历搜索结果所在数据行数组；
            {
                searchResultTable.ImportRow(row);                                                           //数据行导入数据表；
            }
            this.dgv_kc.DataSource = searchResultTable;
        }

        private void tb_szm_TextChanged(object sender, EventArgs e)
        {
            DataRow[] searchResultRows =
               this.csTable1.Select("Acronym LIKE '%" + this.tb_szm.Text.Trim() + "%'");             //借助本窗体的课程数据表的方法Select，并提供与SQL类似的谓词表达式作为查询条件，根据拼音缩写进行模糊查询（仅支持%通配符）；查询将返回数据行数组；
            DataTable searchResultTable = this.csTable1.Clone();                                     //借助本窗体的课程数据表的方法Clone，创建相同架构的空表，用于保存搜索结果所在数据行；
            foreach (DataRow row in searchResultRows)                                                       //遍历搜索结果所在数据行数组；
            {
                searchResultTable.ImportRow(row);                                                           //数据行导入数据表；
            }
            this.dgv_kc.DataSource = searchResultTable;
        }

        private void lb_sz_Click(object sender, EventArgs e)
        {
            
            this.tb_ypmc.Text = this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[1].Value.ToString();
            this.tb_szm.Text = this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[2].Value.ToString();
            this.dtp_scrq.Value = Convert.ToDateTime(this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[3].Value);
            this.dtp_yxq.Value = Convert.ToDateTime(this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[4].Value);
            this.tb_sccs.Text = this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[6].Value.ToString();
            this.tb_dj.Text = this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[8].Value.ToString();
            this.tb_sl.Text = this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[9].Value.ToString();
           
            
        }

        private void tb_sl_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(this.tb_sl.Text.ToString()) > Convert.ToInt32(this.dgv_kc.Rows[this.dgv_kc.CurrentRow.Index].Cells[9].Value.ToString()))
            {
                MessageBox.Show("数量不足，请重新填写！！！");

            }
        }

    }
}
